#include "plateau.h"

#include <QtGui/QBrush>
#include <QtGui/QGraphicsPixmapItem>
#include <QtGui/QGraphicsRectItem>
#include <QtGui/QGraphicsSceneMouseEvent>
#include <QtGui/QGraphicsSimpleTextItem>
#include <QPropertyAnimation>

#include "mainwindow.h"
#include "gcase.h"

#include <QtCore/QDebug>

GPlateau::GPlateau(QObject *parent) :
    QGraphicsScene(parent) {

    QGraphicsPixmapItem *name = new QGraphicsPixmapItem((QPixmap(":/Plateau/Images/name.png")));
    name->setPos(39, -266);

    QGraphicsPixmapItem *plateauAccueil = new QGraphicsPixmapItem((QPixmap(":/Plateau/Images/accueuil.png")));
    plateauAccueil->setPos(53,-113);

    addItem(name);
    addItem(plateauAccueil);

    setBackgroundBrush(QBrush(Qt::black));
}

GPlateau::~GPlateau() {
    for (int i = 0; i < m_listeCases.size(); ++i) {
        if (m_listeCases[i]) {
            delete m_listeCases[i];
            m_listeCases[i] = NULL;
        }
    }
    m_listeCases.clear();

    for (int i = 0; i < m_listeAffichageNbGraines.size(); ++i) {
        if (m_listeAffichageNbGraines[i]) {
            delete m_listeAffichageNbGraines[i];
            m_listeAffichageNbGraines[i] = NULL;
        }
    }
    m_listeAffichageNbGraines.clear();
}

/*void QGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *event) {
    qDebug() << "X: " << event->scenePos().x() << "- Y: " << event->scenePos().y();
}*/

void GPlateau::afficherNouvellePartie(QString pNomJ1, QString pNomJ2) {
    // Suppression des cases existantes
    for (int i = 0; i < m_listeCases.size(); ++i) {
        if (m_listeCases[i]) {
            delete m_listeCases[i];
            m_listeCases[i] = NULL;
        }
    }

    m_listeCases.clear();

    for (int i = 0; i < m_listeAffichageNbGraines.size(); ++i) {
        if (m_listeAffichageNbGraines[i]) {
            delete m_listeAffichageNbGraines[i];
            m_listeAffichageNbGraines[i] = NULL;
        }
    }
    m_listeAffichageNbGraines.clear();

    // On nettoie la zone graphique
    clear();

    m_nomJoueur1 = new QGraphicsTextItem(pNomJ1);
    m_nomJoueur1->adjustSize();
    m_nomJoueur1->setDefaultTextColor(Qt::red);
    m_nomJoueur1->setPos(357.5 - m_nomJoueur1->boundingRect().width() / 2, 20);

    addItem(m_nomJoueur1);

    m_nomJoueur2 = new QGraphicsTextItem(pNomJ2);
    m_nomJoueur2->setDefaultTextColor(Qt::green);
    m_nomJoueur2->setPos(357.5 - m_nomJoueur2->boundingRect().width() / 2, -295);

    addItem(m_nomJoueur2);

    // Mise en place du plateau
    QGraphicsRectItem* plateau = new QGraphicsRectItem(65, -245, 585, 225);
    QBrush b;
    b.setTexture(QPixmap(":/Plateau/Images/textureplateau.png"));
    plateau->setBrush(b);
    addItem(plateau);

    // Mise en place des cases sur le plateau
    m_listeCases.resize(12);
    m_listeCases[0] = new GCase(75,-120, 0, this);
    m_listeCases[1] = new GCase(170,-120, 1, this);
    m_listeCases[2] = new GCase(265,-120, 2, this);
    m_listeCases[3] = new GCase(360,-120, 3, this);
    m_listeCases[4] = new GCase(455,-120, 4, this);
    m_listeCases[5] = new GCase(550,-120, 5, this);
    m_listeCases[6] = new GCase(550,-235, 6, this);
    m_listeCases[7] = new GCase(455,-235, 7, this);
    m_listeCases[8] = new GCase(360,-235, 8, this);
    m_listeCases[9] = new GCase(265,-235, 9, this);
    m_listeCases[10] = new GCase(170,-235, 10, this);
    m_listeCases[11] = new GCase(75,-235, 11, this);

    // On ajoute les cases à la zone graphique
    for (int i = 0; i < m_listeCases.size(); ++i)
        addItem(m_listeCases.at(i));

    // On ajoute l'affichage du nombre de graines sur chaque case
    for (int i=0; i < 12; ++i) {
        QGraphicsSimpleTextItem* tmp = new QGraphicsSimpleTextItem(QString::number(m_listeCases.at(i)->getNbGraines()));
        tmp->setX(m_listeCases.at(i)->x()+40);
        tmp->setY(m_listeCases.at(i)->y()+40);
        tmp->setZValue(2);
        m_listeAffichageNbGraines.append(tmp);
        addItem(tmp);
    }
}

void GPlateau::refreshNbGraines(int pNumCase, int pNbGraines) {
    QGraphicsPixmapItem *tmp = new QGraphicsPixmapItem(QPixmap(":/Plateau/Images/main.png"));
    tmp->setX(m_listeCases[pNumCase]->x());
    tmp->setY(m_listeCases[pNumCase]->y());
    tmp->setZValue(3);
    //addItem(tmp);

    m_listeAffichageNbGraines[pNumCase]->setText(QString::number(pNbGraines));
    m_listeCases[pNumCase]->setNbGraines(pNbGraines);

    delete tmp;
}

void GPlateau::onCaseClicked(GCase *pClickedObject) {
    // On récupère l'index de la case sur laquelle on a cliqué
    int numCase = getNumeroCaseFromObject(pClickedObject);

    if (numCase <= 5 && ((MainWindow*)parent())->isBottomClickable() && m_listeCases[numCase]->getNbGraines() > 0) {
        emit signalCaseChoisie(numCase);
    } else if (numCase >= 6 && numCase <= 11 && ((MainWindow*)parent())->isTopClickable() && m_listeCases[numCase]->getNbGraines() > 0) {
        emit signalCaseChoisie(numCase);
    }
}

int GPlateau::getNumeroCaseFromObject(GCase *pClickedObject) {
    bool found = false;
    int result = -1;
    for(int i = 0; i < m_listeCases.size() && !found; ++i) {
        if (pClickedObject == m_listeCases.at(i)) {
            found = true;
            result = i;
        }
    }
    return result;
}

void GPlateau::jouerTour(int pNumCase) {
    int nbGraines = m_listeCases.at(pNumCase)->getNbGraines();
    m_listeCases.at(pNumCase)->setNbGraines(0);
    refreshNbGraines(pNumCase, 0);

    for (int i = 0; i < nbGraines; i++) {
        int numCase = (pNumCase+i+1)%12;
        m_listeCases.at(numCase)->setNbGraines(m_listeCases.at(numCase)->getNbGraines()+1);
        refreshNbGraines(numCase, m_listeCases.at(numCase)->getNbGraines());
    }
}
